﻿/// Copyright ©2009 Thomas van Veen. All rights reserved
/// Copyright ©2009 Specialwork. All rights reserved

using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.VisualStudio.CommandBars;
using Specialwork.AddIns.VisualStudio.NativeObjectBrowser.Properties;

namespace Specialwork.AddIns.VisualStudio.NativeObjectBrowser
{
    public partial class Connect : Component, IDTExtensibility2, IDTCommandTarget
    {
        #region Properties
        #endregion
        public static DTE2 ApplicationObject { get; set; }
        public static AddIn AddInInstance { get; set; }
        public static Window2 BrowserWindow { get; set; }
        public static OutputWindowPane AddInOutputPane { get; set; }

        #region Enumerations
        #endregion
        public enum Actions
        {
            ShowNativeObjectBrowserWindow,
        }

        #region Construction
        #endregion
        public Connect()
        {
            this.InitializeComponent();
        }
        public Connect(IContainer container)
        {
            container.Add(this);

            this.InitializeComponent();
        }

        #region Events
        #endregion
        public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            try
            {
                Connect.ApplicationObject = (DTE2)application;
                Connect.AddInInstance = (AddIn)addInInst;

                switch (connectMode)
                {
                    case ext_ConnectMode.ext_cm_AfterStartup: break;
                    case ext_ConnectMode.ext_cm_CommandLine: break;
                    case ext_ConnectMode.ext_cm_External: break;
                    case ext_ConnectMode.ext_cm_Solution: break;
                    case ext_ConnectMode.ext_cm_Startup:
                        Connect.InitializeOutputPane();
                        Connect.InitializeCommandBars();

                        break;
                    case ext_ConnectMode.ext_cm_UISetup:
                        Connect.InitializeCommandBars();
                        break;
                }
            }
            catch (Exception exception)
            {
                Connect.WriteLine(exception.Message);
            }
        }
        public void OnAddInsUpdate(ref Array custom)
        {
        }
        public void OnBeginShutdown(ref Array custom)
        {
        }
        public void OnDisconnection(ext_DisconnectMode RemoveMode, ref Array custom)
        {
        }
        public void OnStartupComplete(ref Array custom)
        {
        }

        #region Methods
        #endregion
        public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
        {
            try
            {
                switch (neededText)
                {
                    case vsCommandStatusTextWanted.vsCommandStatusTextWantedName:
                        break;
                    case vsCommandStatusTextWanted.vsCommandStatusTextWantedNone:
                        if (commandName == typeof(Connect).Namespace + ".Connect." + Actions.ShowNativeObjectBrowserWindow)
                        {
                            status = vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;

                            return;
                        }
                        break;
                    case vsCommandStatusTextWanted.vsCommandStatusTextWantedStatus:
                        break;
                    default:
                        break;
                }
            }
            catch (Exception exception)
            {
                Connect.WriteLine(String.Concat("Error: ", exception.Message));
            }
        }
        public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
        {
            try
            {
                handled = false;

                switch (executeOption)
                {
                    case vsCommandExecOption.vsCommandExecOptionDoDefault:
                        if (commandName == typeof(Connect).Namespace + ".Connect." + Actions.ShowNativeObjectBrowserWindow)
                        {
                            handled = true;

                            object objTemp = null;

                            // Create the new tool window, adding your user control.
                            Connect.BrowserWindow = (Window2)((Windows2)ApplicationObject.Windows).CreateToolWindow2(
                                Connect.AddInInstance,
                                Assembly.GetAssembly(typeof(BrowserDocumentControl)).Location,
                                typeof(BrowserDocumentControl).FullName,
                                "Native Object Browser",
                                "{910060ED-6111-4d69-BD45-97BF285837DF}",
                                ref objTemp);

                            Connect.BrowserWindow.Visible = true;

                            return;
                        }
                        break;
                    case vsCommandExecOption.vsCommandExecOptionDoPromptUser:
                        break;
                    case vsCommandExecOption.vsCommandExecOptionPromptUser:
                        break;
                    case vsCommandExecOption.vsCommandExecOptionShowHelp:
                        break;
                    default:
                        break;
                }
            }
            catch (Exception exception)
            {
                Connect.WriteLine(String.Concat("Error: ", exception.Message));
            }
        }

        #region Static Methods
        #endregion
        public static void InitializeOutputPane()
        {
            string productDescription = "Native Object Browser for Microsoft Visual Studio";

            /// Register new output window
            Connect.AddInOutputPane = OutputWindows.CreateOutputPane(Connect.ApplicationObject, productDescription);

            /// Output Welcome message
            Connect.WriteLine(String.Format("Welcome to the {0}", productDescription));
            Connect.WriteLine("");
        }
        public static void InitializeCommandBars()
        {
            try
            {
                object[] contextGUIDS = new object[] { };

                switch (new CultureInfo(Connect.ApplicationObject.LocaleID).TwoLetterISOLanguageName)
                {
                    case "de":
                        {
                            CommandBar commandBar = ((CommandBars)Connect.ApplicationObject.CommandBars)["MenuBar"];
                            CommandBarControl commandBarControl = commandBar.Controls[Resources.deView];
                            CommandBarPopup commandBarPopup = (CommandBarPopup)commandBarControl;
                            Command command = ((Commands2)Connect.ApplicationObject.Commands).AddNamedCommand2(Connect.AddInInstance, Actions.ShowNativeObjectBrowserWindow.ToString(),
                                "Object Browser (Native)", "Öffnet den Object Browser für native Assemblies", true, 60, ref contextGUIDS,
                                (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                                (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                            command.AddControl(commandBarPopup.CommandBar, 1);

                            break;
                        }

                    case "es":
                        break;
                    case "fr":
                        break;
                    case "it":
                        break;
                    case "ja":
                        break;
                    case "ko":
                        break;

                    default:
                        {
                            CommandBar commandBar = ((CommandBars)Connect.ApplicationObject.CommandBars)["MenuBar"];
                            CommandBarControl commandBarControl = commandBar.Controls[Resources.enView];
                            CommandBarPopup commandBarPopup = (CommandBarPopup)commandBarControl;
                            Command command = ((Commands2)Connect.ApplicationObject.Commands).AddNamedCommand2(Connect.AddInInstance, Actions.ShowNativeObjectBrowserWindow.ToString(),
                                "Object Browser (Native)", "Displays the Object Browser for native Assemblies", true, 60, ref contextGUIDS,
                                (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
                                (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
                            command.AddControl(commandBarPopup.CommandBar, 1);

                            break;
                        }
                }
            }
            catch (Exception exception)
            {
                Connect.WriteLine(String.Concat("Error: ", exception.Message));
            }
        }
        public static void WriteLine(string message)
        {
            OutputWindows.WriteLine(Connect.AddInOutputPane, message);
        }
    }
}